#!/usr/bin/perl 

use XML::XQL;
use XML::XQL::DOM;
use Data::Dumper;
use Jcode;
my $parser = new XML::DOM::Parser;


print "Content-type: text/html\n\n";

my $SEARCH_API_URL = "http://www.nal.ie.u-ryukyu.ac.jp/ontolopedia/api/search?keyword=";
my $MATCHFULL_API_URL = "http://www.nal.ie.u-ryukyu.ac.jp/ontolopedia/api/matchfull?keyword=";
my $DATA_API_URL = "http://www.nal.ie.u-ryukyu.ac.jp/ontolopedia/api/show/";


#-- example  --
#検索の例
my @ontrosearch = find('琉球大学');
print "<h3>find(琉球大学)</h3>";
print "<hr><pre>",Dumper(@ontrosearch),"</pre><hr>";

#IDでの検索の例
my @ontroid = getByID(46579);
print "<h3>getByID(46579)</h3>";
print "<hr><pre>",Dumper(@ontroid),"</pre><hr>";

#完全一致検索の例
my @ontroword = getByWord('琉球大学');
print "<h3>getByWord(琉球大学)</h3>";
print "<hr><pre>",Dumper(@ontroword),"</pre><hr>";
#-- example end --

#find('word')
#Ontolopediaから「word」が含まれるものを検索し、その結果を二次元配列で返す
#[0] 検索結果の数
#[*][0] word
#[*][1] id
#[*][2] url
#[*][3] strftime
sub find{
  local $url,$searchword;
  #($search_word) = @_[0];
  $searchword = Jcode->new(\@_[0]);
  #print $searchword->utf8();
  $url = $SEARCH_API_URL;
  $url .= $searchword->utf8();
  #print $url;

  local $getxml = $parser->parsefile($url);
  #取得したXMLファイルを出力してみる
  #print "<hr>",$getxml->toString,"<hr>";

  local (@temp,$id,$word,$url,$strftime,@resultlist);
  local $count=0;
  foreach $results (XML::XQL::solve("ResultList/Result",$getxml)){
    $count = $count+1;
    #print $re->getTagName()."<br>";
     @temp = $results->getElementsByTagName("id");
     $id =  $temp[0]->getFirstChild->getNodeValue;

     @temp = $results->getElementsByTagName("word");
     #$word = $temp[0]->getFirstChild->getNodeValue;
     $word = Jcode->new(\$temp[0]->getFirstChild->getNodeValue);

     @temp = $results->getElementsByTagName("url");
     $url = $temp[0]->getFirstChild->getNodeValue;

     @temp = $results->getElementsByTagName("strftime");
     $strftime = $temp[0]->getFirstChild->getNodeValue;

    #print $count.":".$id.$word.$url.$strftime."<br>";
    
    $resultlist[$count] = [$word->utf8(),$id,$url,$strftime];

    #print "<hr>",$resultlist[$count][1],"<hr>";

  }

  $resultlist[0]=$count;

  #print "<hr><pre>",Dumper(@resultlist),"</pre><hr>";

  return @resultlist;
}


#getByID(id)
#idの概念データを二次元配列で返す関数
#[0][0] target word
#[0][1] target id
#[0][2] target url
#[0][3] target strftime
#[0][4] concept word num
#[*][0] concept word id
#[*][1] concept word word
#[*][2] concept word concept
sub getByID{
  local $url,$word_id;
  ($word_id) = @_[0];
  #print $word_id;
  $url = $DATA_API_URL;
  $url .= $word_id;
  #print $url;

  local $getxml = $parser->parsefile($url);

  local @result;
  local (@temp,$id,$word,$url,$strftime);
  foreach $target (XML::XQL::solve("Ontolopedia/Target",$getxml)){
    #print $re->getTagName()."<br>";
    @temp = $target->getElementsByTagName("id");
    $id =  $temp[0]->getFirstChild->getNodeValue;
    
    @temp = $target->getElementsByTagName("word");
    #$word = $temp[0]->getFirstChild->getNodeValue;
    $word = Jcode->new(\$temp[0]->getFirstChild->getNodeValue);
    
    @temp = $target->getElementsByTagName("url");
    $url = $temp[0]->getFirstChild->getNodeValue;
    
    @temp = $target->getElementsByTagName("strftime");
    $strftime = $temp[0]->getFirstChild->getNodeValue;
    
    #print $count.":".$id.$word.$url.$strftime."<br>";
    $result[0] = [$word->utf8(),$id,$url,$strftime,-1];
  }

  local ($cword,$concept,$from,$id);
  local $count=0;
  foreach $concept_word (XML::XQL::solve("Ontolopedia/conceptwords/word",$getxml)){
    $count = $count+1;
    #$cword = $concept_word->getFirstChild->getNodeValue,"<br>";
    $cword = Jcode->new(\$concept_word->getFirstChild->getNodeValue);
    $concept = $concept_word->getAttribute("concept");
    #$from = $concept_word->getAttribute("from");
    $id =  $concept_word->getAttribute("id");

    $result[$count] = [$id,$cword->utf8(),$concept];
  }

  $result[0][4] = $count;

  #print "<hr><pre>",Dumper(@result),"</pre><hr>";
  return @result;
}


#getByWord('word') 
#wordで完全一致検索した概念データを配列で返す
#[0][0] target word
#[0][1] target id
#[0][2] target url
#[0][3] target strftime
#[0][4] concept word num
#[*][0] concept word id
#[*][1] concept word word     
#[*][2] concept word concept 
sub getByWord{
    local $url,$searchword;
    $searchword = Jcode->new(\@_[0]);
    #print $searchword->utf8();
    $url = $MATCHFULL_API_URL;
    $url .= $searchword->utf8();
    #print $url;

    local $getxml = $parser->parsefile($url);

    local @result;
    local (@temp,$id,$word_temp,$word,$url,$strftime);
    foreach $target (XML::XQL::solve("Ontolopedia/Target",$getxml)){
    #print $re->getTagName()."<br>";
    	@temp = $target->getElementsByTagName("id");
	    $id =  $temp[0]->getFirstChild->getNodeValue;
    
	    @temp = $target->getElementsByTagName("word");
	    #$word_temp = $temp[0]->getFirstChild->getNodeValue;
      #文字コードをutf8にする準備
      $word = Jcode->new(\$temp[0]->getFirstChild->getNodeValue);

	    @temp = $target->getElementsByTagName("url");
	    $url = $temp[0]->getFirstChild->getNodeValue;
    
	    @temp = $target->getElementsByTagName("strftime");
	    $strftime = $temp[0]->getFirstChild->getNodeValue;
    
	    #print $count.":".$id.$word->utf8().$url.$strftime."<br>";
	    $result[0] = [$word->utf8(),$id,$url,$strftime,-1];
    }

    local ($cword_temp,$cword,$concept,$from,$id);
    local $count=0;
    foreach $concept_word (XML::XQL::solve("Ontolopedia/conceptwords/word",$getxml)){
	    $count = $count+1;
	    #$cword_temp = $concept_word->getFirstChild->getNodeValue,"<br>";
      $cword = Jcode->new(\$concept_word->getFirstChild->getNodeValue);

	    $concept = $concept_word->getAttribute("concept");
       #$from = $concept_word->getAttribute("from");
	    $id =  $concept_word->getAttribute("id");

	    $result[$count] = [$id,$cword->utf8(),$concept];
    }

    $result[0][4] = $count;

  #print "<hr><pre>",Dumper(@result),"</pre><hr>";
    return @result;
}

